package com.aferzsoft.colorslide.classes;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class GameSurface extends SurfaceView implements SurfaceHolder.Callback{

	public final static float START_VELOCITY = 1;
	
	private boolean isStarted = false;
	
	private float surfaceHeight;
	private float surfaceWidth;
	
	private SurfaceThread thread;
	//private Handler handler;
	
	private Grid grid;
	
	public GameSurface(Context context, AttributeSet attrs) {
        super(context, attrs);
        getHolder().addCallback(this);
    }
	
	public GameSurface(Context context) {
		super(context);
		getHolder().addCallback(this);
	}
	
	public Grid getGrid(){
		return this.grid;
	}
	
	public void startGame(){
		this.grid = new Grid( 5, 5, this.surfaceWidth, this.surfaceHeight );
	}

	public void endGame(){
		
	}

	@Override
	public void onDraw(Canvas canvas){
		//Clear canvas
		canvas.drawColor( Color.TRANSPARENT, PorterDuff.Mode.CLEAR );
		
		//Draw Grid
		this.grid.onDraw(canvas);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return true;
	}
	
	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		//setWillNotDraw(false); 								//Allows us to use invalidate() to call onDraw()
		if( !this.isStarted ) this.startGame();
		
		this.thread = new SurfaceThread(getHolder(), this); 	
		this.thread.setRunning(true);                     	
		this.thread.start();                              	
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		boolean retry = true;
		while(retry){
			try{
				this.thread.setRunning(false);
				this.thread.join();
				retry = false;
			}catch(InterruptedException e){ }
		}
	}

	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
	    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

	    int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
	    int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
	    this.surfaceHeight = parentHeight;
	    this.surfaceWidth = parentWidth;
	    this.setMeasuredDimension( parentWidth , parentHeight);
	}
	
	
	public class SurfaceThread extends Thread{
		
		private SurfaceHolder sHolder;
	    private GameSurface cSurface;
	    private boolean run = false;
	    
	    public SurfaceThread(SurfaceHolder surfaceHolder, GameSurface cSurface) {
	        this.sHolder = surfaceHolder;
	        this.cSurface = cSurface;
	    }
	    
	    public void setRunning(boolean run) { 
	        this.run = run;
	    }
	    
	    @Override
	    public void run() {
	    	Canvas canvas;
	    	while (this.run) {
	    		canvas = null;      		
	            try {
	            	canvas = this.sHolder.lockCanvas(null);
	            	if( canvas != null){
		                synchronized (this.sHolder) {
		                	cSurface.onDraw(canvas); //postInvalidate();
		                }
	            	}
	            } finally {
	                if (canvas != null) {
	                    this.sHolder.unlockCanvasAndPost(canvas);
	                }
	            }
	        }
	    }

	}
	
}